package com.atcumt.lcSolution;

import java.util.Random;

// 随机输出给定的数字的索引
public class LC8 {

    int[] nums;
    Random rd;

    public static void main(String[] args) {
        int[] nums = new int[] {1,2,3,3,3};
        LC8 solution = new LC8(nums);
        solution.pick(2);
    }

    public LC8 (int[] nums) {
        this.nums = nums;
        rd = new Random();
    }

    public void pick(int target) {

        int res = 0;
        int cnt = 0; // 记录遇到 target 的次数
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                cnt++;

                if (rd.nextInt(cnt) == 0) {
                    res = i; // 生成 0~cnt 的随机数
                }
            }
        }
        System.out.println(res);
    }
}
